# -*- coding:utf-8 -*-
import functools
from werkzeug.routing import BaseConverter
from flask import session, jsonify, g
from iHome.utils.response_code import RET


class RegexConverter(BaseConverter):
    """自定义正则转换器"""

    def __init__(self, url_map, *args,**kwargs):
        super(RegexConverter, self).__init__(url_map)
        self.regex = args[0]



def login_required(f):
    """
    用户是否登录的装饰器判断
    :param f:
    :return:
    """
    # 修饰内层函数，以防当前修饰器去修改被装饰函数的__name__属性
    @functools.wraps(f)
    def wrapper(*args, **kwargs):
        user_id = session.get('user_id')
        if not user_id:
            return jsonify(errno=RET.SESSIONERR, errmsg="用户未登录")
        else:
            g.user_id = user_id
            return f(*args, **kwargs)

    return wrapper